<?php

/**
 * This is the model class for table "solucionxproblema".
 *
 * The followings are the available columns in table 'solucionxproblema':
 * @property integer $idmotivo
 * @property integer $idsolucion
 * @property integer $cantidad
 */
class Solucionxproblema extends CActiveRecord
{
    
    public $distrito;
      public $causa;
      public $sugerencia;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Solucionxproblema the static model class
	 */
	
    
    public static function getListDistrito()
    {
        return CHtml::listData(Distrito::model()->findAll(),'id', 'descripcion');
                   
    }
    public function soluciones($distrito){
        $sort = new CSort();
        $sort->attributes = array(
            'id',
            'causa',
            'cantidad',
            'sugerencia',
            'distrito',
        );

        
        $sql="select  a.id, a.descripcion causa, c.cantidad cantidad , d.descripcion sugerencia,c.distrito distrito
                from
                (SELECT idmotivo,distrito, count( * ) cantidad
                FROM incidente 
                
                GROUP BY idmotivo,distrito ) C, solucion d , solucionxproblema e, causa_incidente a
                where c.cantidad > e.cantidad and d.idsolucion=e.idsolucion  and c.idmotivo= e.idmotivo and a.id=c.idmotivo";
        mysql_connect("localhost", "root", "");
        
        mysql_select_db("tesis") or die ("Verifique la Base de Datos");
       /* $result=mysql_query($sql);
        //mysql_fetch_object($result)
       while ($row = mysql_fetch_assoc($result)) {
            $filas [] = $row;
        }
//        for ($i = 0; $i < count($fila); $i++) {
//        $arreglo;
//        $arreglo[$i]=array( "causa" => $fila['causa'], "cantidad"=>$fila['cantidad'] , "sugerencia" => $fila['sugerencia']);
//            
//        }
        mysql_close();
        return $filas;*/
        $rawData=Yii::app()->db->createCommand($sql)->queryAll();
        $dataProvider=new CArrayDataProvider(
            $rawData,
            array(
                'sort' => $sort,
            )
        );
        return $dataProvider;
    }
    
    
    public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'solucionxproblema';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idmotivo, idsolucion, cantidad', 'required'),
			array('idmotivo, idsolucion, cantidad', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idmotivo, idsolucion, cantidad', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idmotivo' => 'Idmotivo',
			'idsolucion' => 'Idsolucion',
			'cantidad' => 'Cantidad',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idmotivo',$this->idmotivo);
		$criteria->compare('idsolucion',$this->idsolucion);
		$criteria->compare('cantidad',$this->cantidad);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}